package com.shengdou.as.slopsapptest.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.base.BaseActivity;
import com.shengdou.as.slopsapptest.bean.AboutMineBean;
import com.shengdou.as.slopsapptest.utils.Constants;
import com.shengdou.as.slopsapptest.utils.HttpUtils;
import com.shengdou.as.slopsapptest.utils.LogUtil;
import com.shengdou.as.slopsapptest.utils.MyListViewUtils;
import com.shengdou.as.slopsapptest.utils.NetConstant;

import java.util.ArrayList;

/**
 * 关于我们
 */
public class AboutMineActivity extends BaseActivity {
    private ListView mListView;
    private LinearLayout gone;
    private MyAdapter adapter;
    private ArrayList<AboutMineBean.DataBean> array = new ArrayList<>();
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constants.ABOUTMINE:
                    String results = msg.obj.toString();
                    LogUtil.e("关于我们", results);
                    AboutMineBean aboutMineBean = new Gson().fromJson(results, AboutMineBean.class);
                    if (aboutMineBean.getData() != null && aboutMineBean.getData().size() > 0) {
                        array.clear();
                        array.addAll(aboutMineBean.getData());
                        if (array != null && array.size() > 0) {
                            mListView.setVisibility(View.VISIBLE);
                            gone.setVisibility(View.GONE);
                            mListView.setAdapter(adapter);
                            adapter.setmArray(array);
                        } else {
                            adapter.notifyDataSetChanged();
                            mListView.setVisibility(View.GONE);
                            gone.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_mine);
        init();
    }

    private void init() {
        mListView = (ListView) findViewById(R.id.listview);
        gone = (LinearLayout) findViewById(R.id.txt_gone);
        adapter = new MyAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpUtils.post(mHandler, NetConstant.ABOUTMINE, Constants.ABOUTMINE, AboutMineActivity.this);
    }

    public void onTabClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    class MyAdapter extends BaseAdapter {
        private ArrayList<AboutMineBean.DataBean> mArray;

        public MyAdapter() {
        }

        public void setmArray(ArrayList<AboutMineBean.DataBean> mArray) {
            this.mArray = mArray;
        }

        @Override
        public int getCount() {
            return mArray != null ? mArray.size() : 0;
        }

        @Override
        public AboutMineBean.DataBean getItem(int position) {
            return mArray.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(AboutMineActivity.this, R.layout.item_about_mine, null);
                holder.mTxtTitle = (TextView) convertView.findViewById(R.id.txt_title);
                holder.listview = (MyListViewUtils) convertView.findViewById(R.id.listview);
                holder.mView = convertView.findViewById(R.id.view);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTxtTitle.setText(mArray.get(position).getTitle());
            ChildAdapter childAdapter = new ChildAdapter();
            childAdapter.setmArray(mArray.get(position).getValue());
            holder.listview.setAdapter(childAdapter);
            final int index = position;
            if (index == mArray.size() - 1) {
                holder.mView.setVisibility(View.GONE);
            }
            return convertView;
        }
    }

    class ViewHolder {
        TextView mTxtTitle;
        TextView mTxtValue;
        View mView;
        MyListViewUtils listview;
    }

    class ChildAdapter extends BaseAdapter {
        private ArrayList<AboutMineBean.DataBean.ValueBean> mArray;

        public ChildAdapter() {
        }

        public void setmArray(ArrayList<AboutMineBean.DataBean.ValueBean> mArray) {
            this.mArray = mArray;
        }

        @Override
        public int getCount() {
            return mArray != null ? mArray.size() : 0;
        }

        @Override
        public AboutMineBean.DataBean.ValueBean getItem(int position) {
            return mArray.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(AboutMineActivity.this, R.layout.item_about_child_mine, null);
                holder.mTxtValue = (TextView) convertView.findViewById(R.id.txt_value);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTxtValue.setText("\u3000" + mArray.get(position).getVal());

            return convertView;
        }
    }
}
